/* global FormData */
/* global getCookie */
/* global template */
/* global advTypeId */
/* global timeToTimestamp */
var typeDetail = {};
$.get('/antique/api/mallAdvInfo/query_adv_type_info', { id:advTypeId }, function (result) {
    if (result.code == 0 && result.data) {
        $("#advtitle").html("你选择的是" + result.data.typeDesc );
        typeDetail = result.data;
    }
});

$.get('/antique/api/mallProduct/manage/query_list', { limit:9999,token:getCookie('token'),state:'1' }, function (result) {
    if (result.code == 0) {
        if (result.data && result.data.list && result.data.list.length > 0) {
            var list = result.data.list;
            for (var i = 0; i < list.length;i++) { 
                $("#keyId").append("<option value='" + list[i].id + "'>" + list[i].name + "</option>");
            }
            
        }
    }
});

function addAdv() {
    var startDate = $("#startDate").val();
    var keyType = $("#keyType").children('option:selected').val();
    var count = $("#count").val();
    var image = $("#image").val();
    
    if (startDate == ''){
        $.toast("开始日期不能为空", "cancel");
        return;
    }
    if (count == ''){
        $.toast("天数不能为空", "cancel");
        return;
    }
    if (image == ''){
        $.toast("图片不能为空", "cancel");
        return;
    }
    
    var params = {};
    params.advTypeId =  $("#advTypeId").val();
    params.startDate = timeToTimestamp(startDate);
    params.count = count;
    params.keyType = keyType;
    params.keyId = $("#keyId").children('option:selected').val();    
    params.token = getCookie('token');
    params.image = image;
    if (keyType == 1) {
        delete params.keyId;//店铺ID //TODO
    }

    $.ajax({
        type:'POST',
        url:"/antique/api/mallAdvInfo/create_adv",
        data: params,
        dataType:"json",
        success:function(res){
            if (res.code == 0){
                if (res.data.needMoney && parseFloat(res.data.needMoney) > 0) {
                    $("#payMoney").html(res.data.needMoney);
                    $("#payId").val(res.data.id);
                    $("#pay_deposit").popup();
                } else {
                    var params = {};
                    params.id = res.data.id;
                    params.token = getCookie('token');
                    submitAdd(params);
                }
            } else {
                $.toast(res.message, "cancel");
            }            
        }
    });          
}

/* global servicePay */
$("#confirmBondPay").click(function(){
    var type = $("input[name='radioPay']:checked").val();
    var id = $("#payId").val();
    var payMoney = $("#payMoney").html();
    if (id == ''){
        $.toast("参数错误", "cancel");
        return;
    }
    if (type == 1) {
        passwordPay(id);
    } else if (type == 2){
        /* global isMiniProgram wx */
        if ( isMiniProgram () ) {//小程序就跳转小程序支付
            var params = {
                type: 150,//支付类型
                id: id,//关联id
                payMoney: payMoney,//支付金额
                content:'发布广告',//订单说明
                jumpUrl:'/pages/adv_manage',//支付成功后跳转地址  为空返回当前页面 不可以带 ? 和 &
                };
            wx.miniProgram.navigateTo({
                url: "/pages/pay/index?params=" + JSON.stringify(params)
            });
        } else {
            $.post('/antique/api/onlinePay/wxpay/create_wxpay', {
                token:getCookie('token'),
                type:150,
                appSource:1,
                keyId:id,
                money:servicePay(payMoney),
                content:'发布广告' + payMoney
            }, function (result) {
                var appId = result.data.appid;
                var nonceStr = result.data.nonce_str;
                var prepay_id = result.data.prepay_id;
                var mch_id = result.data.mch_id;
                /* global goWeiPay */
                goWeiPay(appId,nonceStr,prepay_id,mch_id);
            });
        }
    }

});

function passwordPay (id) {
    $.modal({
        title: "请输入支付密码",
        text: '<input class="weui-input weui-prompt-input" type="password" id="password"  placeholder="没有设置密码可不填" />',
        buttons: [ 
            { text: "取消", className: "default" },
            { text: "确定", onClick: function(){
            var password = $("#password").val();
            var params = {};
            params.id = id;
            params.token = getCookie('token');
            var time = timeToTimestamp(new Date());
            if (password == '') {
                params.payPass = hex_md5(time + '').toUpperCase(); 
            } else {
                var passwords = hex_md5(password).toUpperCase();
                params.payPass = hex_md5(passwords + time).toUpperCase();
            }
            params.time = time;
            submitAdd(params);
            } }
            
        ]
    });
}

/* global timeToTimestamp  hex_md5*/
function submitAdd(params){
    $.ajax({
        type:'POST',
        url:"/antique/api/mallAdvInfo/pay_adv",
        data: params,
        dataType:"json",
        success:function(res){
            if (res.code == 0){
                $.toast("成功");
             } else {
                $.toast(res.message, "cancel");
            }
            
        }
    });
      

}
  
$( "#keyType" ).change(function() {
    var keyType = $(this).children('option:selected').val();
    if (keyType == 2) {
        $( "#allPro" ).show();
    } else {
        $( "#allPro" ).hide();
    }

    });
$( "#count" ).on("input",function(e){
    $("#totle").html(e.delegateTarget.value * typeDetail.money );
});
    


